package cn.xm.basic.service.impl;

import cn.xm.basic.config.BussinessException;
import cn.xm.basic.constant.BaseConstant;
import cn.xm.basic.service.RegisterService;
import cn.xm.basic.util.MD5Utils;
import cn.xm.basic.util.StrUtils;
import cn.xm.user.domain.Logininfo;
import cn.xm.user.domain.User;
import cn.xm.user.dto.PhoneRegisterDto;
import cn.xm.user.mapper.LogininfoMapper;
import cn.xm.user.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.sql.Struct;
import java.util.Date;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    public void phoneRegister(PhoneRegisterDto dto) {
        // 验证参数
        verifyParam(dto);
        // 获取user
        User user = dtoConvertUser(dto);
        // 获取logininfo
        Logininfo logininfo = userConvertLoginfo(user);
        // 添加loigninfo
        logininfoMapper.add(logininfo);
        // 设置user的logininfoId
        user.setLogininfoId(logininfo.getId());
        // 添加user
        userMapper.add(user);
    }

    private User dtoConvertUser(PhoneRegisterDto dto) {
        // 盐值
        String salt = StrUtils.getRandomString(32);
        String password = salt + dto.getPassword();
        // 加密
        String encrypByMd5 = MD5Utils.encrypByMd5(password);

        return User.builder()
                .username(dto.getPhone())
                .phone(dto.getPhone())
                .salt(salt)
                .password(encrypByMd5)
                .state(1)
                .createtime(new Date())
                .build();
    }

    private Logininfo userConvertLoginfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        return logininfo;
    }

    private void verifyParam(PhoneRegisterDto dto) {
        // 非空判断
        // 两次手机号是否一致
        // 验证码是否正确
        String redisSmsCode = (String)redisTemplate.opsForValue().get(String.format(BaseConstant.verifyCodeCons.REGISTER_SMSCODE_PHOEN, dto.getPhone()));
        if (StringUtils.isBlank(redisSmsCode)) {
            throw new BussinessException("验证码错误");
        }
        if (!redisSmsCode.equals(dto.getSmsCode())) {
            throw new BussinessException("验证码错误");
        }
        // 两次密码是否一致
        if (!dto.getPassword().equals(dto.getPasswordRepeat())) {
            throw new BussinessException("两次密码不一致");
        }
        // 手机是否注册过
        // 是否注册过
        User user = userMapper.findByPhone(dto.getPhone());
        if (null != user) {
            throw new BussinessException("手机号已注册！");
        }
    }
}
